package com.csglxt.base;

import java.lang.reflect.ParameterizedType;
import java.util.Map;

import javax.annotation.Resource;

import com.csglxt.domain.User;
import com.csglxt.service.GoodsCateService;
import com.csglxt.service.GoodsService;
import com.csglxt.service.MarketService;
import com.csglxt.service.NoticeService;
import com.csglxt.service.OnshelvesInfoService;
import com.csglxt.service.OrderService;
import com.csglxt.service.StorageService;
import com.csglxt.service.SupplierService;
import com.csglxt.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @ClassName: BaseAction
 * @Description: 基础控制器
 * @author Delver_Si
 * @date 2015-5-11 下午10:29:12
 * 
 * @param <T>
 */
@SuppressWarnings({ "unchecked", "serial" })
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

    // ========ModelDriven的支持=================

    protected T model;

    public BaseAction() {
        try {
            // 通过反射得到model的真实类型
            ParameterizedType pt = (ParameterizedType) this.getClass()
                    .getGenericSuperclass();
            Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
            // 通过反射创建model的实例
            model = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public T getModel() {
        return model;
    }

    /**
     * 获取当前登录的用户
     * 
     * @return
     */
    protected User getCurrentUser() {
        return (User) ActionContext.getContext().getSession().get("user");
    }

    /**
     * 获取值栈
     * 
     * @return
     */
    protected ValueStack getValueStack() {
        return ActionContext.getContext().getValueStack();
    }

    /**
     * 获取Session
     * 
     * @return
     */
    protected Map<String, Object> getSession() {
        return ActionContext.getContext().getSession();
    }

    // ============== 分页用的参数 =============

    protected int pageNum = 1; // 当前页
    protected int pageSize = 8; // 每页显示多少条记录

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    // ========Service实例的声明=================

    @Resource
    protected UserService userService;

    @Resource
    protected SupplierService supplierService;

    @Resource
    protected MarketService marketService;

    @Resource
    protected StorageService storageService;

    @Resource
    protected NoticeService noticeService;

    @Resource
    protected GoodsService goodsService;

    @Resource
    protected GoodsCateService goodsCateService;

    @Resource
    protected OrderService orderService;

    @Resource
    protected OnshelvesInfoService onshelvesInfoService;
}
